package com.demo.testpackage;

import com.demo.entity.A;
import com.demo.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class Test {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        Student student1 = new Student("1", "张三1");
        Student student2 = new Student("2", "张三2");
        Student student3 = new Student("3", "张三3");
        Student student4 = new Student("4", "张三4");
        Student student5 = new Student("5", "张三5");
        Student student6 = new Student("6", "张三6");
        Student student7 = new Student("6", "张三5");
        list.add(student2);
        list.add(student4);
        list.add(student6);
        list.add(student3);
        list.add(student5);
        list.add(student1);
        list.add(student7);
        log.info("排序前：");
        list.stream().forEach(student -> System.out.println(student));
        log.info("排序后：");
        list.stream().sorted(Comparator.comparing(Student::getId).thenComparing(Student::getName))
                .forEach(student -> System.out.println(student));

        list.stream().map(Student::getId).distinct().collect(Collectors.toList())
                .forEach(s -> System.out.println(s));

        A a1 = new A("1", "2022-01-02");
        A a2 = new A("2", "2022-01-03");
        A a3 = new A("3", "2022-01-04");
        A a4 = new A("4", "2022-01-05");
        List<A> list1 = new ArrayList<>();
        list1.add(a1);
        list1.add(a2);
        list1.add(a3);
        list1.add(a4);
        List<String> timeList = list1.stream().map(A::getTime).collect(Collectors.toList());
        timeList.replaceAll(a->stringToString(a,"yyyy-MM-dd","yyyyMMdd"));
        timeList.forEach(s -> System.out.println(s));
        String y = "2021-01-05";
        String date = stringToString(y, "yyyy-MM-dd", "yyyyMMdd");
//        System.out.println(date);
    }
    public static String stringToString(String dateStr, String inputFormatter,String outputFormatter) {
        if (StringUtils.isNotBlank(dateStr)) {
            LocalDate date =  LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(inputFormatter));
            return date.format(DateTimeFormatter.ofPattern(outputFormatter));
        }
        return null;
    }
}
